﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace CodeTestFramework
{
    public class UnitTestBase<T>
    {
        protected T Instance;

        private TestContext testContextInstance;

        //used to call the base methods of TestContext
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void TestInitialize()
        {
            
            Type curType = this.GetType();
            Assembly curAssembly = curType.Assembly;
            AnswerAttribute[] answers = (AnswerAttribute[])curType.GetCustomAttributes(typeof(AnswerAttribute), true);
            foreach (var answer in answers)
            {
                string type = answer.Namespace + '.' + answer.Participant;
                if (curAssembly.GetType(type) != null)
                {
                    this.Instance = (T)curAssembly.CreateInstance(type);
                }
                //else
                //{
                //    throw new NotImplementedException();
                //}
            }
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void TestCleanup()
        {
            if (this.Instance is IDisposable)
            {
                IDisposable disposable = (IDisposable)this.Instance;
                disposable.Dispose();
            }
        }

    }

    [AttributeUsage(AttributeTargets.Class,AllowMultiple =true)]
    public class AnswerAttribute : Attribute
    {
        string strNamespace;

        public string Namespace
        {
            get { return strNamespace; }
            set { strNamespace = value; }
        }

        string strParticipant;

        public string Participant
        {
            get { return strParticipant; }
            set { strParticipant = value; }
        }
    }



}
